package com.nisco.dms.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nisco.dms.entity.SqlColumn;
import com.nisco.dms.entity.SqlTable;
import com.nisco.dms.exception.ServiceException;
import com.nisco.dms.service.SqlColumnService;
import com.nisco.dms.service.SqlTableService;
import com.nisco.dms.util.auto.AutoUtil;
import com.nisco.dms.util.dwz.AjaxObject;
import com.nisco.dms.util.dwz.Page;

/**
 * 
 * @author Robin
 * 
 */
@Controller
@RequestMapping(value = "sqltable")
public class SqlTableController {
	@Autowired
	private SqlTableService sqlTableService;
	@Autowired
	private SqlColumnService sqlColumnService;
	private static final String CREATE = "sqltable/sqltableCreate";
	private static final String UPDATE = "sqltable/sqltableUpdate";
	private static final String LIST = "sqltable/sqltableList";
	private static final String MAIN = "sqltable/sqlmain";
	private static final String IMPORTCODE = "sqltable/importCode";
	@RequiresPermissions("SqlTable:save")
	@RequestMapping(value = "/main", method = RequestMethod.GET)
	public String main(Map<String, Object> map) {
		SqlTable sqlTable = sqlTableService.findByStatus();
		map.put("sqlTable", sqlTable);
		return MAIN;
	}

	@RequiresPermissions("SqlTable:save")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String preCreate() {
		return CREATE;
	}

	@RequiresPermissions("SqlTable:save")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public @ResponseBody
	String create(SqlTable sqlTable, HttpServletRequest request) {
		sqlTable.setStatus("N");
		sqlTableService.save(sqlTable);

		AjaxObject ajaxObject = new AjaxObject(200, "添加成功！", "", "", "",
				"closeCurrent");
		return ajaxObject.toString();
	}

	@RequiresPermissions("SqlTable:edit")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String preUpdate(@PathVariable String id, Map<String, Object> map) {
		SqlTable sqlTable = sqlTableService.get(id);

		map.put("sqlTable", sqlTable);
		return UPDATE;
	}

	@RequiresPermissions("SqlTable:edit")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody
	String update(SqlTable sqlTable) {
		sqlTable.setStatus("N");
		sqlTableService.update(sqlTable);
		
		AjaxObject ajaxObject = new AjaxObject(200, "修改成功！", "", "", "",
				"closeCurrent");
		return ajaxObject.toString();
	}

	@RequiresPermissions("SqlTable:delete")
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public @ResponseBody
	String delete(@PathVariable String id) {
		AjaxObject ajaxObject = new AjaxObject("删除成功！");
		try {
			sqlTableService.delete(id);
		} catch (ServiceException e) {
			ajaxObject.setStatusCode(AjaxObject.STATUS_CODE_FAILURE);
			ajaxObject.setMessage("删除失败：" + e.getMessage());
		}
		// 必须设置callBackType否则会调制主页面
		ajaxObject.setCallbackType("");
		return ajaxObject.toString();
	}

	@RequiresPermissions("SqlTable:view")
	@RequestMapping(value = "/list", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String list(Page page, String keywords, Map<String, Object> map) {
		List<SqlTable> sqlTables = null;

		if (StringUtils.isNotBlank(keywords)) {
			sqlTables = sqlTableService.find(keywords, page);
		} else {
			sqlTables = sqlTableService.findAll(page);
		}

		map.put("page", page);
		map.put("sqlTables", sqlTables);
		map.put("keywords", keywords);

		return LIST;
	}
	@RequestMapping(value = "/codeimport/{id}", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String importCode(@PathVariable String id, Map<String, Object> map){
		SqlTable sqlTable = sqlTableService.findOne(id);
		List<SqlColumn> sqlColumns = sqlColumnService.findBySqlTable(sqlTable);
		//自动生成
		AutoUtil autoUtil = new AutoUtil(sqlTable, sqlColumns);
		autoUtil.runAuto();
		
		map.put("sqlTable", sqlTable);
		return IMPORTCODE;
	}
}
